﻿package com.oyjd.ybx.front.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestUtils;

import com.oyjd.ybx.bean.vo.ProLineVO;
import com.oyjd.ybx.bean.vo.UserVO;
import com.oyjd.ybx.business.classify.service.ClassifyService;
import com.oyjd.ybx.business.talk.service.TalkService;
import com.oyjd.ybx.business.talk.vo.TalkExVO;
import com.oyjd.ybx.fw.action.BaseAction;
import com.oyjd.ybx.fw.util.CodeUtil;
import com.oyjd.ybx.fw.util.OutUtil;
import com.oyjd.ybx.fw.util.RandUtil;
import com.oyjd.ybx.pub.Const;

/**
 * 
 * 在线交流Action控制层
 * 
 * @author <b>欧建东<br/>
 *         Email:oyjd@163.com</b>
 * @version 1.0
 */
@Action(value = "/frontTalk")
@Results({ @Result(name = "goTalk", location = Const.JSP_front_goTalk) })
public class FrontTalkAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 152347027015039L;

	private static final Log log = LogFactory.getLog(FrontTalkAction.class);
	private TalkService service;
	private ClassifyService classifyService;

	@Autowired
	public void setClassifyService(ClassifyService classifyService) {
		this.classifyService = classifyService;
	}

	@Autowired
	public void setService(TalkService service) {
		this.service = service;
	}

	/**
	 * 获取在线交流列表数据
	 * 
	 * @return
	 */
	public String goTalk() {
		// 分类树
		getRequest().setAttribute("treeMap", classifyService.getFrontClass());
		return "goTalk";
	}

	/**
	 * 获取在线交流列表数据
	 * 
	 * @return
	 */
	public String getList() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			String getAccount = ServletRequestUtils.getStringParameter(req, "getAccount");

			String talkAccount;
			UserVO userVO = (UserVO) session.get(CodeUtil.SessionKey);
			if (null == userVO) {
				talkAccount = req.getSession().getId();
			} else {
				talkAccount = userVO.getAccount();
			}
			List<TalkExVO> list = service.getList(talkAccount, getAccount);

			//
			map.put("data", list);
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 新增在线交流
	 * 
	 * @return
	 */
	public String add() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			String getAccount = ServletRequestUtils.getStringParameter(req, "getAccount");
			String content = ServletRequestUtils.getStringParameter(req, "content");

			String talkAccount;
			UserVO userVO = (UserVO) session.get(CodeUtil.SessionKey);
			if (null == userVO) {
				talkAccount = req.getSession().getId();
			} else {
				talkAccount = userVO.getAccount();
			}
			service.add(talkAccount, getAccount, content);
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 获取在线专家
	 * 
	 * @return
	 */
	public String getPro() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			List<ProLineVO> list = service.getPro();
			//
			map.put("data", RandUtil.getRandList(list, 3));
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}
}
